Skip to content

[libc] Add hooks for extra options in running hermetic tests #147931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 15, 2025

Conversation

saturn691
Copy link
Contributor

@saturn691 saturn691 commented Jul 10, 2025

Part of #145349. These hooks are required downstream in order to run hermetic tests. See arm/arm-toolchain#420 for more context on how this PR will be used.

@saturn691 saturn691 requested review from petrhosek and jhuber6 July 10, 2025 10:17
@saturn691 saturn691 marked this pull request as ready for review July 11, 2025 09:51
@llvmbot llvmbot added the libc label Jul 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2025

@llvm/pr-subscribers-libc

Author: William Huynh (saturn691)

Changes

These hooks are required downstream in order to run hermetic tests. I'm not too sure about the specifics about this PR, it's quite rough, but if you're happy we can get started with hermetic testing downstream.

See arm/arm-toolchain#420 for more context on how this PR will be used.

Closes #145349. This is the final PR to get minimal functionality.


Full diff: https://github.com/llvm/llvm-project/pull/147931.diff

1 Files Affected:

  • (modified) libc/cmake/modules/LLVMLibCTestRules.cmake (+20-5)
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 1cd09816e223f..739fd6639a976 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -288,7 +288,9 @@ function(create_libc_unittest fq_target_name)
   target_include_directories(${fq_build_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR})
   target_include_directories(${fq_build_target_name} PRIVATE ${LIBC_SOURCE_DIR})
   target_compile_options(${fq_build_target_name} PRIVATE ${compile_options})
-  target_link_options(${fq_build_target_name} PRIVATE ${link_options})
+  
+  string(REPLACE " " ";" EXTRA_LINK_OPTIONS "${LIBC_TEST_EXTRA_LINK_OPTIONS}")
+  target_link_options(${fq_build_target_name} PRIVATE ${link_options} ${EXTRA_LINK_OPTIONS})
 
   if(NOT LIBC_UNITTEST_CXX_STANDARD)
     set(LIBC_UNITTEST_CXX_STANDARD ${CMAKE_CXX_STANDARD})
@@ -580,7 +582,9 @@ function(add_integration_test test_name)
       -march=${LIBC_GPU_TARGET_ARCHITECTURE} -nostdlib -static
       "--cuda-path=${LIBC_CUDA_ROOT}")
   elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
-    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
+    string(REPLACE " " ";" EXTRA_LINK_OPTIONS "${LIBC_TEST_EXTRA_LINK_OPTIONS}")
+    target_link_options(${fq_build_target_name} PRIVATE
+      -nolibc -nostartfiles -nostdlib++ -static ${EXTRA_LINK_OPTIONS})
   else()
     # Older version of gcc does not support `nostdlib++` flag.  We use
     # `nostdlib` and link against libgcc_s, which cannot be linked statically.
@@ -774,7 +778,9 @@ function(add_libc_hermetic test_name)
       -march=${LIBC_GPU_TARGET_ARCHITECTURE} -nostdlib -static
       "--cuda-path=${LIBC_CUDA_ROOT}")
   elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
-    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
+    string(REPLACE " " ";" EXTRA_LINK_OPTIONS "${LIBC_TEST_EXTRA_LINK_OPTIONS}")
+    target_link_options(${fq_build_target_name} PRIVATE 
+      -nolibc -nostartfiles -nostdlib++ -static ${EXTRA_LINK_OPTIONS})
   else()
     # Older version of gcc does not support `nostdlib++` flag.  We use
     # `nostdlib` and link against libgcc_s, which cannot be linked statically.
@@ -809,9 +815,16 @@ function(add_libc_hermetic test_name)
   endif()
 
   if(NOT HERMETIC_TEST_NO_RUN_POSTBUILD)
-    set(test_cmd ${HERMETIC_TEST_ENV}
+    if (LIBC_TEST_CMD)
+      # In the form of "<command> binary=@BINARY@", e.g. "qemu-system-arm -loader$<COMMA>file=@BINARY@"
+      string(REPLACE "@BINARY@" "$<TARGET_FILE:${fq_build_target_name}>" test_cmd_parsed ${LIBC_TEST_CMD})
+      string(REPLACE " " ";" test_cmd "${test_cmd_parsed}")
+    else()
+      set(test_cmd ${HERMETIC_TEST_ENV}
         $<$<BOOL:${LIBC_TARGET_OS_IS_GPU}>:${gpu_loader_exe}> ${CMAKE_CROSSCOMPILING_EMULATOR} ${HERMETIC_TEST_LOADER_ARGS}
         $<TARGET_FILE:${fq_build_target_name}> ${HERMETIC_TEST_ARGS})
+    endif()
+
     add_custom_target(
       ${fq_target_name}
       DEPENDS ${fq_target_name}.__cmd__
@@ -863,7 +876,9 @@ function(add_libc_test test_name)
       # Tests like the file tests perform file operations on disk file. If we
       # don't chain up the unit test and hermetic test, then those tests will
       # step on each other's files.
-      add_dependencies(${fq_test_name}.__hermetic__ ${fq_test_name}.__unit__)
+      if(NOT LIBC_TEST_HERMETIC_ONLY)
+        add_dependencies(${fq_test_name}.__hermetic__ ${fq_test_name}.__unit__)
+      endif()
     endif()
   endif()
 endfunction(add_libc_test)

Comment on lines 292 to 293
string(REPLACE " " ";" EXTRA_LINK_OPTIONS "${LIBC_TEST_EXTRA_LINK_OPTIONS}")
target_link_options(${fq_build_target_name} PRIVATE ${link_options} ${EXTRA_LINK_OPTIONS})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have LIBC_COMPILE_OPTIONS_DEFAULT and LIBC_TEST_COMPILE_OPTIONS_DEFAULT. Does it work for you, or a separate linking flag is actually needed? If so can we add LIBC_LINK_OPTIONS_DEFAULT and LIBC_TEST_LINK_OPTIONS_DEFAULT similar to the compile option flags?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that should work for me. I'll get to work on it next week, we may need to keep the string replace due to way our build system uses ExternalProject_Add.

@saturn691 saturn691 merged commit 0425a5d into llvm:main Jul 15, 2025
19 checks passed
@saturn691 saturn691 deleted the libc-test branch July 15, 2025 10:43
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 15, 2025

LLVM Buildbot has detected a new failure on builder premerge-monolithic-linux running on premerge-linux-1 while building libc at step 7 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/153/builds/38152

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
...
PASS: UBSan-ThreadSanitizer-x86_64 :: TestCases/ImplicitConversion/unsigned-integer-truncation-ignorelist.c (98555 of 101600)
PASS: lld :: COFF/color-diagnostics.test (98556 of 101600)
PASS: lld :: COFF/comdat-jumptable.s (98557 of 101600)
PASS: lld :: COFF/cl-gl.test (98558 of 101600)
PASS: lld :: COFF/cgprofile-icf.s (98559 of 101600)
PASS: cfi-standalone-lld-x86_64 :: base-derived-destructor.cpp (98560 of 101600)
PASS: flang-OldUnit :: Evaluate/real.test (98561 of 101600)
PASS: lld :: COFF/arm64ec-entry-mangle.test (98562 of 101600)
PASS: lld :: COFF/arm64x-export.test (98563 of 101600)
TIMEOUT: MLIR :: Examples/standalone/test.toy (98564 of 101600)
******************** TEST 'MLIR :: Examples/standalone/test.toy' FAILED ********************
Exit Code: 1
Timeout: Reached timeout of 60 seconds

Command Output (stdout):
--
# RUN: at line 1
"/etc/cmake/bin/cmake" "/build/buildbot/premerge-monolithic-linux/llvm-project/mlir/examples/standalone" -G "Ninja"  -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C_COMPILER=/usr/bin/clang  -DLLVM_ENABLE_LIBCXX=OFF -DMLIR_DIR=/build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir  -DLLVM_USE_LINKER=lld  -DPython3_EXECUTABLE="/usr/bin/python3.10"
# executed command: /etc/cmake/bin/cmake /build/buildbot/premerge-monolithic-linux/llvm-project/mlir/examples/standalone -G Ninja -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C_COMPILER=/usr/bin/clang -DLLVM_ENABLE_LIBCXX=OFF -DMLIR_DIR=/build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir -DLLVM_USE_LINKER=lld -DPython3_EXECUTABLE=/usr/bin/python3.10
# .---command stdout------------
# | -- The CXX compiler identification is Clang 16.0.6
# | -- The C compiler identification is Clang 16.0.6
# | -- Detecting CXX compiler ABI info
# | -- Detecting CXX compiler ABI info - done
# | -- Check for working CXX compiler: /usr/bin/clang++ - skipped
# | -- Detecting CXX compile features
# | -- Detecting CXX compile features - done
# | -- Detecting C compiler ABI info
# | -- Detecting C compiler ABI info - done
# | -- Check for working C compiler: /usr/bin/clang - skipped
# | -- Detecting C compile features
# | -- Detecting C compile features - done
# | -- Looking for histedit.h
# | -- Looking for histedit.h - found
# | -- Found LibEdit: /usr/include (found version "2.11") 
# | -- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
# | -- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.13") 
# | -- Using MLIRConfig.cmake in: /build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir
# | -- Using LLVMConfig.cmake in: /build/buildbot/premerge-monolithic-linux/build/lib/cmake/llvm
# | -- Linker detection: unknown
# | -- Performing Test LLVM_LIBSTDCXX_MIN
# | -- Performing Test LLVM_LIBSTDCXX_MIN - Success
# | -- Performing Test LLVM_LIBSTDCXX_SOFT_ERROR
# | -- Performing Test LLVM_LIBSTDCXX_SOFT_ERROR - Success
# | -- Performing Test CXX_SUPPORTS_CUSTOM_LINKER
# | -- Performing Test CXX_SUPPORTS_CUSTOM_LINKER - Success
# | -- Performing Test C_SUPPORTS_FPIC
# | -- Performing Test C_SUPPORTS_FPIC - Success
# | -- Performing Test CXX_SUPPORTS_FPIC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants